<?php defined('SYSPATH') OR die('No direct access allowed.');

class Url_Plugin extends WB_FormItem {
	
	protected $db_type;			// valid: int, text
	
	public function __construct($name='', $params=array())
	{
		parent::__construct($name, $params, 'text');
	}
	
	// TODO: validation
	public function prep_db()
	{
		return 
			valid::url($this->value) ? 
				$this->value:
				null;
	}
	
	public function set_value($value, $type)
	{
		if ( ! empty($value) && ($type=='add' || $type=='edit'))
		{
			if ( ! valid::url($value) )
			{
				if ( valid::url('http://'.$value)) 
				{
					$this->value = 'http://'.$value;
					return;
				}
				Kohana::instance()->user_messages->add(Kohana::lang('url_plugin.not_valid', $this->label), 'error');
				$this->value = NULL;
				return;
			}
		}
		parent::set_value($value, $type);
	}
	
	public function render_cell()
	{
		return empty($this->value) ? '&nbsp;' : html::anchor($this->value); 
	}
}

//End of Url_Plugin Library